package com.ftg.learn.controller;

import com.ftg.learn.dao.Danpin;
import com.ftg.learn.service.DanpinService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@CrossOrigin
@RequestMapping("/dan")
public class DanpinController {

    @Autowired
    DanpinService danpinService;

    @GetMapping("/select")
    public Map<String,Object> select(int page , int limit, String name){
        Map<String ,Object>  map=new HashMap<>();
        List<Danpin> list=danpinService.select(page,limit,name);
        map.put("data",list);
        map.put("name",name);
        map.put("code","0");
        map.put("count",danpinService.getCount(name));
        return map;
    }
    @GetMapping("/showall")
    public Map<String,Object> select(){
        Map<String ,Object>  map=new HashMap<>();
        List<Danpin> list=danpinService.showall();
        map.put("data",list);

        map.put("code","0");

        return map;
    }

    @GetMapping("/picture")
    public Map<String,Object> selectpicture(){
        Map<String ,Object>  map=new HashMap<>();
        List<Danpin> list=danpinService.selectpicture();
        map.put("data",list);
        map.put("code","0");
        return map;
    }

    //添加
    @PostMapping("/add")
    public int add(Danpin danpin){
        danpin.setId(UUID.randomUUID().toString());
        System.out.println(danpin.getName());
        return danpinService.add(danpin);
    }
    //删除
    @DeleteMapping("/delete/{id}")
    public int delete(@PathVariable  String id){

        return  danpinService.delete(id);
    }
    //修改
    @PostMapping("/update")
    public int update(Danpin danpin){
        return  danpinService.update(danpin);
    }


}
